/*
 * NetWorkMain.h
 *
 *  Created on: 2017年5月25日
 *      Author: orange
 */

#ifndef NETWORKMAIN_H_
#define NETWORKMAIN_H_

#include <semaphore.h>
#include <string>

#include "../public/concurrentqueue.h"
#include "../public/event/SendSocketJuzEvent.h"
#include "../public/Plugin.h"
#include "../public/PublicDefine.h"

class ClientQuitJuzEvent;

class OnCommandJuzEvent;

class NetWorkMain :public Plugin
{
private :
	NetWorkMain();
public:

	virtual ~NetWorkMain();
	static NetWorkMain *getInstance();

    void eventListen(JuzEvent *event); //监听的事件 回调!
    void bindFinsh();

    void OnDisable();//注销
    void OnEnable();

    void OnCommand(OnCommandJuzEvent *ev);

    void InitSocket();
    void clientQuit(ClientQuitJuzEvent *ev);
    void closeSocket_Juz(int fd);//关闭socket

    int decryptedDataInfo(char *key,JUZ_SOCKET_DATA &sockData);//解密数据
    void sendData(SendSocketJuzEvent *sendEv);


    static void SendClientQUitEv(int fd,int nState);

    static void* socketTcpCheck(void *args);//TCP检查
    static void* socketWaitData(void *args);//socket 检查数据
    static void* socketJuzAccept(void *args); //accept
    static void* socketRecv(void *args);//recv
    static void* sendSocketInfo(void *args); //send
    static bool  decryptSocketData(SocketInfoJuzEvent *sock,char *key );

    static void  setAttribute(int fd,bool status); //属性修改
    static bool  RC4_SOCKET(char *key,int nLen,char *src,char *out);
    static char* randKey(char *src,int nLen);


private:
    static NetWorkMain *mNetWorkMain;

public:
    static const int MAXEPOLLSIZE; //epoll最大句柄数
    static const int MAXLINE;//监听最大数
    static const int MPORT;//服务端口
    static const int RECVTHREADNUM;//接受线程数
    static const int SENDTHREADNUM;//发送线程
    static const int TCPCHECKTIME;//TCP 僵尸链接能存活的时间
    static const int UNITEVERMCSERVER;//统一版本
    static const int UNITEVERMANDROID;//android 版本

    static int mEpollFd;

   moodycamel::ConcurrentQueue<int>  mRecvList; //这里面的都是可以准备recv的
   HashMapSocket mAllClientInfo;
   sem_t *recv_sem; //接受信号量
   sem_t *send_sem;//发送信号

   //发送的事件
   moodycamel::ConcurrentQueue<JUZSENDINFO>  mSendList;//发送的链表

   //tcp检查
   HashMapTcpCheck mCheckList;
};

extern "C"
{
	Plugin *loadSoPlugin();
}

#endif /* NETWORKMAIN_H_ */
